package ai.zixing.mashibing.basic_class.class02;

public class Code02_DeleteGivenValue {

    public static Node removeValue(Node head, int num) {
        // 可能删除的节点就是头节点，先处理头节点
        while (head != null) {
            if (head.value != num) {
                break;
            }
            head = head.next;
        }
        // 找到第一个不是 num 的节点
        Node pre = head;
        Node cur = head;
        while (cur != null) {
            // 如果有多个 value，则pre的next一直后移，直到不是 value时，将 pre = cur
            if (cur.value == num) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
}
